/**
 * 2013 2013-6-27
 */
package com.surfilter.platform.base.util;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.surfilter.platform.base.model.ModelBase;

/**
 * 
 *         用户获取request中的各种参数，并组成指定的类型返回的工具类
 * 
 */
public class ParaUtil {
	private static Log log = LogFactory.getLog(ParaUtil.class);

	/**
	 * 从request中获取键为key指定类型的的参数
	 * @param req
	 * @param key
	 * @param clz
	 * @return
	 */
	public static <T> T getPara(HttpServletRequest request, String key, Class<T> clz) {
		return getPara1(request,key,clz,true);
	}
	
	
	private static <T> T getPara1(HttpServletRequest request, String key, Class<T> clz,boolean raiseError) {
		String s = request.getParameter(key);
		if (s != null) {
			Object o = ObjectUtil.getBasicObjectValue(clz, s);
			return (T) o;
		} else {
			if(raiseError)
			log.error("can't find parameter '" + key + "' in  request");
			return null;
		}
	}
	
	/**
	 * 从request中获取某个bean对应的字段组装返回新的bean实例
	 * @param request
	 * @param clz
	 * @return
	 */
	public static <T extends ModelBase> T getBean(HttpServletRequest request,Class<T> clz){
		T instance = null;
		try {
			instance = (T)clz.newInstance();
			Set<Field> fields = new HashSet<Field>();
			for (Class c = clz; c != null; c = c.getSuperclass()) {
				if(Object.class.getName().equals(c.getName()))
					break;
				fields.addAll(Arrays.asList(c.getDeclaredFields()));
			}
			for (Field f : fields) {
				String name = f.getName();
				Object e = getPara1(request, name, f.getType(),false);
				if(e != null && !"".equals(e)){   //在request中有对应的参数
					Method setter = ObjectUtil.getSetterMethod(clz, f);
					if(setter != null ){    //存在setter方法
						setter.invoke(instance, e);
					}else{       //不存在setter方法，尝试直接注入属性
						if(f.isAccessible()){
							f.set(instance, e);
						}else{
							f.setAccessible(true);
							f.set(instance, e);
							f.setAccessible(false);
						}
					}
				}
			}
		}catch(Exception e){
			log.error(e);
			return null;
		}
		return instance;
	}
	
	/**
	 * 从request将某个key对应的json类型字符串转化为POJO
	 * @param request
	 * @param key
	 * @param clz
	 * @return
	 */
	public static <T extends ModelBase> T getJSONObj(HttpServletRequest request,String key, Class<T> clz){
		String value = request.getParameter(key);
		if(value != null){
			Object obj = ObjectUtil.getObjectBeanFromJsonObjectString(clz, value);
			return (T) obj;
		}else{
			log.error("can't find parameter '" + key + "' in  request");
			return null;
		}
	}
	/**
	 * 从request将某个key对应的json类型字符串转化为clz的list
	 * @param request
	 * @param key
	 * @param clz
	 * @return
	 */
	public static <T> List<T> getJSONList(HttpServletRequest request,String key, Class<T> clz){
		String value = request.getParameter(key);
		if(value != null){
			JSONArray array = JSONArray.fromObject(value);
			Collection c = JSONArray.toCollection(array, clz);
			List<T> retList = new ArrayList<T>(c);
			return retList;
		}else{
			log.error("can't find parameter '" + key + "' in  request");
			return null;
		}
	}
	
	
	
}
